package top.huxy.boot.api.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import top.huxy.boot.api.WeatherApi;
import top.huxy.boot.model.vo.VOWeather;
import top.huxy.boot.model.vo.VOWeatherForecase;
import top.huxy.boot.util.IPAddressUtils;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * @author admin
 */
@Component
@Slf4j
public class WeatherApiImpl implements WeatherApi {

    @Autowired
    RestTemplate restTemplate;

    @Override
    public VOWeather getNowWeather(String ip) {
        String city = getCityName(ip);

        String now = null;
        try {
            now = restTemplate.getForObject("https://restapi.amap.com/v3/weather/weatherInfo?key=b97d27ec70637b832a43d3438ff65881&extensions=base&city=" + city, String.class);
        } catch (Exception e) {
            log.error("restTemplate Error", e);
            return new VOWeather("错误");
        }


        JSONObject jsonObject = JSONObject.parseObject(now);
        JSONArray arr = jsonObject.getJSONArray("lives");
        if (arr.isEmpty()) {
            log.info("未知的天气数据,ip={},city={},res={}", ip, city, now);
            return new VOWeather("未知");
        }

        JSONObject lives = arr.getJSONObject(0);
        String cityName = lives.getString("city");
        String weather = lives.getString("weather");
        String windPower = lives.getString("windpower");
        String temperature = lives.getString("temperature");
        String reportTime = lives.getString("reporttime");
        String windDirection = lives.getString("winddirection");
        VOWeather voWeather = new VOWeather();
        voWeather.setTime(reportTime);
        voWeather.setWeather(weather);
        voWeather.setTemperature(temperature);
        voWeather.setWindDir(windDirection);
        voWeather.setWindPower(windPower);
        voWeather.setWindPower(windPower);
        voWeather.setCity(cityName);

        return voWeather;
    }

    private String getCityName(String ip) {
        String city = "北京";
        try {
            //中国|湖北省|武汉市|电信，内网IP|内网IP
            String cityInfo = IPAddressUtils.getCityInfo(ip);
            if (cityInfo != null) {
                String[] split = cityInfo.split("\\|");
                if (split.length > 2) {
                    city = split[split.length - 2];
                } else {
                    log.debug("split.length <= 2,ip={},split={}", ip, split);
                }
            } else {
                log.debug("cityInfo == null,ip={}", ip);
            }
        } catch (Exception ignored) {
        }
        return city;
    }

    @Override
    public List<VOWeatherForecase> getWeatherForecast(String ip) {
        String city = getCityName(ip);
        String resString = null;
        try {
            resString = restTemplate.getForObject("https://restapi.amap.com/v3/weather/weatherInfo?key=b97d27ec70637b832a43d3438ff65881&extensions=all&city=" + city, String.class);
        } catch (Exception e) {
            log.error("restTemplate Error", e);
            return Collections.singletonList(new VOWeatherForecase("错误"));
        }


        JSONObject response = JSONObject.parseObject(resString);
        JSONArray forecasts = response.getJSONArray("forecasts");
        JSONObject dataObject = forecasts.getJSONObject(0);
        JSONArray casts = dataObject.getJSONArray("casts");
        LinkedList<VOWeatherForecase> res = new LinkedList<>();
        String cityName = dataObject.getString("city");
        for (Object o : casts) {
            JSONObject json = (JSONObject) o;

            String date = json.getString("date");
            String dayWeather = json.getString("dayweather");
            String dayWind = json.getString("daywind");
            String week = json.getString("week");
            String dayPower = json.getString("daypower");
            String dayTemp = json.getString("daytemp");
            String nightWind = json.getString("nightwind");
            String nighTemp = json.getString("nighttemp");
            String nightWeather = json.getString("nightweather");
            String nightPower = json.getString("nightpower");

            VOWeatherForecase voWeatherForecase = new VOWeatherForecase();
            voWeatherForecase.setCity(cityName);
            voWeatherForecase.setDate(date);
            voWeatherForecase.setDayWeather(dayWeather);
            voWeatherForecase.setDayWind(dayWind);
            voWeatherForecase.setWeek(week);
            voWeatherForecase.setDayPower(dayPower);
            voWeatherForecase.setDayTemp(dayTemp);
            voWeatherForecase.setNightWind(nightWind);
            voWeatherForecase.setNighTemp(nighTemp);
            voWeatherForecase.setNightWeather(nightWeather);
            voWeatherForecase.setNightPower(nightPower);
            res.add(voWeatherForecase);
        }
        return res;
    }
}
